package com.example.mediaplayerdemo.gestures.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

import com.example.mediaplayerdemo.R;

public class GesturesActivity extends AppCompatActivity {
    private GestureDetector detector;
    private MyGestureListener listener;
    private final static String TAG = "TAG";
    private final static int MIN_MOVE = 200;   //最小距离
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gestures);
        //实例化GestureListener与GestureDetector对象
        listener = new MyGestureListener();
        detector = new GestureDetector(this, listener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    private class MyGestureListener implements GestureDetector.OnGestureListener {

        @Override
        public boolean onDown(MotionEvent motionEvent) {
            Log.i(TAG, "onDown:按下");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {
            Log.i(TAG, "onShowPress:手指按下一段时间,不过还没到长按");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            Log.i(TAG, "onSingleTapUp:手指离开屏幕的一瞬间");
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            Log.i(TAG, "onScroll:在触摸屏上滑动");
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {
            Log.i(TAG, "onLongPress:长按并且没有松开");
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float vx, float vy) {
            Log.i(TAG, "onFling:迅速滑动，并松开");
            Log.i(TAG, "e1-y:" + e1.getY() + ", e2-y:" + e2.getY());
            if (e1.getY() - e2.getY() > MIN_MOVE) {
                startActivity(new Intent(GesturesActivity.this, GesturesActivity.class));
                Toast.makeText(GesturesActivity.this, "通过手势启动Activity", Toast.LENGTH_SHORT).show();
            } else if (e1.getY() - e2.getY() < MIN_MOVE) {
                finish();
                Toast.makeText(GesturesActivity.this, "通过手势关闭Activity", Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    }
}